var util = require("../../../utils/util.js");
var qcloud = require('../../../vendor/wafer2-client-sdk/index');
var config = require('../../../config');
// pages/my/ordeDetail/ordeDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    this.setData({
      order_id: options.order_id
    })
    console.log(options.order_id);
    console.log(options.isShare);
    wx.setNavigationBarTitle({
      title: '详细信息'
    })
    if(options.isShare == 1){
      console.log("来自分享");
      var userInfo = wx.getStorageSync('userInfo');
      console.log(userInfo);
      if (!userInfo){
        console.log("不存在用户信息，需要登录");
        // 调用登录接口
        qcloud.login({
          success(result) {
            if (result) {
              console.log(result + "登陆接口login");
              console.log(result);
              //首次登录，返回用户信息，不请求用户信息接口获取信息
              util.showSuccess('登录成功1')
              var userId = result.uid;
              that.setData({
                userInfo: result,
                logged: true
              })
              // 存储用户信息到本地
              wx.setStorage({
                key: 'userInfo',
                data: {
                  userInfo: result,
                  bType: "warn",
                  actionText: "退出登录"
                },
                success: function (res) {
                  console.log("存储成功2")
                },
              })
              var _uid = result.uid
              var order_id = this.data.order_id;
              that.requestGetOrderDetail(_uid, order_id);
              that.requestSetMyRecipient(_uid, order_id);
            } else {
              // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
              qcloud.request({
                url: config.service.requestUrl,//server/user?type=customer
                method: "GET",
                login: true,
                success(result) {
                  console.log(result);
                  console.log(result + "走用户信息接口");
                  console.log('请求用户信息接口成功2');
                  if (result.data.code == 0) {
                    var userId = result.data.data.uid;
                    util.showSuccess('登录成功2')
                    that.setData({
                      userInfo: result.data.data,
                      logged: true
                    })
                    // 存储用户信息到本地
                    wx.setStorage({
                      key: 'userInfo',
                      data: {
                        userInfo: result.data.data,
                        bType: "warn",
                        actionText: "退出登录"
                      },
                      success: function (res) {
                        console.log("存储成功2")
                      },
                    })
                    var _uid = result.data.data.uid
                    var order_id = this.data.order_id;
                    that.requestGetOrderDetail(_uid, order_id);
                    that.requestSetMyRecipient(_uid, order_id);
                  } else {
                    console.log(result.data.msg);
                  }
                },
                fail(error) {
                  console.log('请求用户信息接口失败');
                  util.showModel('请求失败1', error)
                  console.log('request fail', error)
                }
              })
            }
          },
          fail(error) {
            util.showModel('登录失败', error)
            console.log('登录失败', error)
          }
        })
      }else{
        console.log("存在用户信息userInfo");
        var that = this;
        var _uid = wx.getStorageSync("userInfo").userInfo.uid
        var order_id = this.data.order_id;
        console.log(_uid);
        console.log(order_id)
        that.requestSetMyRecipient(_uid, order_id);
        that.requestGetOrderDetail(_uid, order_id);
      }
    }else{
      var _uid = wx.getStorageSync("userInfo").userInfo.uid
      var order_id = this.data.order_id;
      that.requestGetOrderDetail(_uid,order_id);
    }
  },

  //  获取当前订单信息
   requestGetOrderDetail:function(_uid,order_id){
     var that = this;
     wx.request({
       url: "https://boxapi.supergk.com/server/order/going_order_detail",
       data: {
         order_id: order_id,
         user_id: _uid
       },
       success: function (res) {
         console.log(res);
         if (res.data.code == 0) {
           console.log("请求")
           console.log(res)
           that.setData({
             orderDetailInfo: res.data.data,
           })
         }
       },
       fail: function (res) { },
       complete: function (res) { },
     })
   },

  //  设置收件人
  //  https://boxapi.supergk.com/server/order/add_receiver_by_share
   requestSetMyRecipient: function (_uid, _order_id){
     var that = this;
     wx.request({
       url: "https://boxapi.supergk.com/server/order/add_receiver_by_share",
       header: {
         "Content-Type": "application/x-www-form-urlencoded"
       },
       method: "POST",
       data: util.json2Form({ order_id: _order_id, uid: _uid}),
       success: function (res) {
         console.log(res);
         if (res.data.code == 1) {
           console.log(res);
           wx.showModal({
             title: '提示',
             content: res.data.msg,
           })
          //  wx.navigateTo({
          //    url: '../../index/index',
          //  })
         } else {
           console.log(res.data.msg)
         }
       }
     })
   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})